﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DockingLib.Docking;
using MS.Internal.Mita.Foundation;

namespace UIAStudio
{
    public partial class MainForm : Form
    {
        private static MainForm _instance;
        private static Hotkey m_CaptureHotKey;
        private DeserializeDockContent m_deserializeDockContent;
        private ProjectExplorer m_projectExplorer;
        private TreeWalker m_treeWalker;
        private PropertiesSheet m_PropertiesSheet;

        private IContainer components = null;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem treeWalkerToolStripMenuItem;
        private DockingLib.Docking.DockPanel dockPanel1;
        private System.Windows.Forms.ToolStripMenuItem propertiesToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem tToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem projectExplorerToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem setTreeWalkerFilterToolStripMenuItem;
        
        public MainForm()
        {
            _instance = this;
            //m_docView.TopLevel = false;
            InitializeComponent();
            
            m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
            m_projectExplorer = new ProjectExplorer();
            m_treeWalker = new TreeWalker();
            m_PropertiesSheet = new PropertiesSheet();
            
            
            
            //m_docView.Show(this);
            //m_docView.Show(dockPanel1);

            

            
        }

        void m_uiaTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            UIATreeNode uiNode = e.Node as UIATreeNode;
            UIObject item = uiNode.UIObject;
            PropertyListWorkItem propListWorkItem = new PropertyListWorkItem(WorkQueue, this.m_PropertiesSheet, item);
            HighlightWorkItem hightlightWorkItem = new HighlightWorkItem(WorkQueue, item);
            WorkQueue.Enqueue(propListWorkItem);
            WorkQueue.Enqueue(hightlightWorkItem);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (File.Exists(ConfigFilePath))
            {
                dockPanel1.LoadFromXml(ConfigFilePath, m_deserializeDockContent);
            }
            RegisterHotKey();
            m_treeWalker.UIATreeView.AfterSelect += new TreeViewEventHandler(m_uiaTreeView_AfterSelect);
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (persistString == typeof(TreeWalker).ToString())
            {
                return m_treeWalker;
            }
            else if (persistString == typeof(PropertiesSheet).ToString())
            {
                return m_PropertiesSheet;
            }
            else
            {
                Type t = Type.GetType(persistString);
                IDockContent dockWnd = Activator.CreateInstance(t) as IDockContent;
                return dockWnd;
            }
        }

        private string ConfigFilePath
        {
            get
            {
                string configPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                configPath = Path.Combine(configPath, "UIAStudio");
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }
                configPath = System.IO.Path.Combine(configPath, "UIAStudioLayout.config");

                return configPath;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnRegisterHotKey();
            Helper.DisposeWorkQueue();

            //close document windows to valid it save layout in config file
            IDockContent[] documents = dockPanel1.Documents.ToArray();
            foreach(IDockContent doc in documents)
            {
                doc.DockHandler.Close();
            }
            
            dockPanel1.SaveAsXml(ConfigFilePath);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x312)
            {  
                m_CaptureHotKey.Invoke();
            }
            base.WndProc(ref m);
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_PropertiesSheet.Show(dockPanel1);
        }
   
        private void RegisterHotKey()
        {
            if (m_CaptureHotKey == null)
            {
                m_CaptureHotKey = new Hotkey("Capture hotkey", this.Handle, ModKeys.Win, Keys.C, new EventHandler(this.CaptureProc));
                m_CaptureHotKey.Register();
            }
        }

        private void UnRegisterHotKey()
        {
            if (m_CaptureHotKey != null)
            {
                m_CaptureHotKey.Unregister();
            }
        }

        public void CaptureProc(object obj, EventArgs args)
        {
            Point curPosition = Cursor.Position;
            UIObject search = UIObject.FromPoint(curPosition);
            m_treeWalker.UIATreeView.SearchTree(search);
        }

        private void treeWalkerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_treeWalker.Visible)
            {
                m_treeWalker.Show(dockPanel1);
                //m_treeWalker.Show(dockPanel1, DockState.DockLeft);
            }
        }

        private void tToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UIEditor ui = new UIEditor("a.xml");
            ui.Show(dockPanel1);
        }

        private void projectExplorerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_treeWalker.Visible)
            {
                m_projectExplorer.Show(dockPanel1);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            DockingLib.Docking.DockPanelSkin dockPanelSkin1 = new DockingLib.Docking.DockPanelSkin();
            DockingLib.Docking.AutoHideStripSkin autoHideStripSkin1 = new DockingLib.Docking.AutoHideStripSkin();
            DockingLib.Docking.DockPanelGradient dockPanelGradient1 = new DockingLib.Docking.DockPanelGradient();
            DockingLib.Docking.TabGradient tabGradient1 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.DockPaneStripSkin dockPaneStripSkin1 = new DockingLib.Docking.DockPaneStripSkin();
            DockingLib.Docking.DockPaneStripGradient dockPaneStripGradient1 = new DockingLib.Docking.DockPaneStripGradient();
            DockingLib.Docking.TabGradient tabGradient2 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.DockPanelGradient dockPanelGradient2 = new DockingLib.Docking.DockPanelGradient();
            DockingLib.Docking.TabGradient tabGradient3 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.DockPaneStripToolWindowGradient dockPaneStripToolWindowGradient1 = new DockingLib.Docking.DockPaneStripToolWindowGradient();
            DockingLib.Docking.TabGradient tabGradient4 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.TabGradient tabGradient5 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.DockPanelGradient dockPanelGradient3 = new DockingLib.Docking.DockPanelGradient();
            DockingLib.Docking.TabGradient tabGradient6 = new DockingLib.Docking.TabGradient();
            DockingLib.Docking.TabGradient tabGradient7 = new DockingLib.Docking.TabGradient();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.projectExplorerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.treeWalkerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.propertiesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.tToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.setTreeWalkerFilterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.dockPanel1 = new DockingLib.Docking.DockPanel();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.viewToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(487, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "&File";
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(92, 22);
            this.exitToolStripMenuItem.Text = "&Exit";
            // 
            // viewToolStripMenuItem
            // 
            this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.projectExplorerToolStripMenuItem,
            this.treeWalkerToolStripMenuItem,
            this.propertiesToolStripMenuItem,
            this.tToolStripMenuItem,
            this.setTreeWalkerFilterToolStripMenuItem});
            this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
            this.viewToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.viewToolStripMenuItem.Text = "&View";
            // 
            // projectExplorerToolStripMenuItem
            // 
            this.projectExplorerToolStripMenuItem.Name = "projectExplorerToolStripMenuItem";
            this.projectExplorerToolStripMenuItem.Size = new System.Drawing.Size(184, 22);
            this.projectExplorerToolStripMenuItem.Text = "Project &Explorer";
            this.projectExplorerToolStripMenuItem.Click += new System.EventHandler(this.projectExplorerToolStripMenuItem_Click);
            // 
            // treeWalkerToolStripMenuItem
            // 
            this.treeWalkerToolStripMenuItem.Name = "treeWalkerToolStripMenuItem";
            this.treeWalkerToolStripMenuItem.Size = new System.Drawing.Size(184, 22);
            this.treeWalkerToolStripMenuItem.Text = "&Tree Walker";
            this.treeWalkerToolStripMenuItem.Click += new System.EventHandler(this.treeWalkerToolStripMenuItem_Click);
            // 
            // propertiesToolStripMenuItem
            // 
            this.propertiesToolStripMenuItem.Name = "propertiesToolStripMenuItem";
            this.propertiesToolStripMenuItem.Size = new System.Drawing.Size(184, 22);
            this.propertiesToolStripMenuItem.Text = "&Properties";
            this.propertiesToolStripMenuItem.Click += new System.EventHandler(this.propertiesToolStripMenuItem_Click);
            // 
            // tToolStripMenuItem
            // 
            this.tToolStripMenuItem.Name = "tToolStripMenuItem";
            this.tToolStripMenuItem.Size = new System.Drawing.Size(184, 22);
            this.tToolStripMenuItem.Text = "T";
            this.tToolStripMenuItem.Click += new System.EventHandler(this.tToolStripMenuItem_Click);
            // 
            // setTreeWalkerFilterToolStripMenuItem
            // 
            this.setTreeWalkerFilterToolStripMenuItem.Name = "setTreeWalkerFilterToolStripMenuItem";
            this.setTreeWalkerFilterToolStripMenuItem.Size = new System.Drawing.Size(184, 22);
            this.setTreeWalkerFilterToolStripMenuItem.Text = "Set Tree Walker Filter";
            // 
            // dockPanel1
            // 
            this.dockPanel1.ActiveAutoHideContent = null;
            this.dockPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dockPanel1.DockBackColor = System.Drawing.SystemColors.Control;
            this.dockPanel1.Location = new System.Drawing.Point(0, 24);
            this.dockPanel1.Name = "dockPanel1";
            this.dockPanel1.Size = new System.Drawing.Size(487, 353);
            dockPanelGradient1.EndColor = System.Drawing.SystemColors.ControlLight;
            dockPanelGradient1.StartColor = System.Drawing.SystemColors.ControlLight;
            autoHideStripSkin1.DockStripGradient = dockPanelGradient1;
            tabGradient1.EndColor = System.Drawing.SystemColors.Control;
            tabGradient1.StartColor = System.Drawing.SystemColors.Control;
            tabGradient1.TextColor = System.Drawing.SystemColors.ControlDarkDark;
            autoHideStripSkin1.TabGradient = tabGradient1;
            autoHideStripSkin1.TextFont = new System.Drawing.Font("Segoe UI", 9F);
            dockPanelSkin1.AutoHideStripSkin = autoHideStripSkin1;
            tabGradient2.EndColor = System.Drawing.SystemColors.ControlLightLight;
            tabGradient2.StartColor = System.Drawing.SystemColors.ControlLightLight;
            tabGradient2.TextColor = System.Drawing.SystemColors.ControlText;
            dockPaneStripGradient1.ActiveTabGradient = tabGradient2;
            dockPanelGradient2.EndColor = System.Drawing.SystemColors.Control;
            dockPanelGradient2.StartColor = System.Drawing.SystemColors.Control;
            dockPaneStripGradient1.DockStripGradient = dockPanelGradient2;
            tabGradient3.EndColor = System.Drawing.SystemColors.ControlLight;
            tabGradient3.StartColor = System.Drawing.SystemColors.ControlLight;
            tabGradient3.TextColor = System.Drawing.SystemColors.ControlText;
            dockPaneStripGradient1.InactiveTabGradient = tabGradient3;
            dockPaneStripSkin1.DocumentGradient = dockPaneStripGradient1;
            dockPaneStripSkin1.TextFont = new System.Drawing.Font("Segoe UI", 9F);
            tabGradient4.EndColor = System.Drawing.SystemColors.ActiveCaption;
            tabGradient4.LinearGradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
            tabGradient4.StartColor = System.Drawing.SystemColors.GradientActiveCaption;
            tabGradient4.TextColor = System.Drawing.SystemColors.ActiveCaptionText;
            dockPaneStripToolWindowGradient1.ActiveCaptionGradient = tabGradient4;
            tabGradient5.EndColor = System.Drawing.SystemColors.Control;
            tabGradient5.StartColor = System.Drawing.SystemColors.Control;
            tabGradient5.TextColor = System.Drawing.SystemColors.ControlText;
            dockPaneStripToolWindowGradient1.ActiveTabGradient = tabGradient5;
            dockPanelGradient3.EndColor = System.Drawing.SystemColors.ControlLight;
            dockPanelGradient3.StartColor = System.Drawing.SystemColors.ControlLight;
            dockPaneStripToolWindowGradient1.DockStripGradient = dockPanelGradient3;
            tabGradient6.EndColor = System.Drawing.SystemColors.InactiveCaption;
            tabGradient6.LinearGradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
            tabGradient6.StartColor = System.Drawing.SystemColors.GradientInactiveCaption;
            tabGradient6.TextColor = System.Drawing.SystemColors.InactiveCaptionText;
            dockPaneStripToolWindowGradient1.InactiveCaptionGradient = tabGradient6;
            tabGradient7.EndColor = System.Drawing.Color.Transparent;
            tabGradient7.StartColor = System.Drawing.Color.Transparent;
            tabGradient7.TextColor = System.Drawing.SystemColors.ControlDarkDark;
            dockPaneStripToolWindowGradient1.InactiveTabGradient = tabGradient7;
            dockPaneStripSkin1.ToolWindowGradient = dockPaneStripToolWindowGradient1;
            dockPanelSkin1.DockPaneStripSkin = dockPaneStripSkin1;
            this.dockPanel1.Skin = dockPanelSkin1;
            this.dockPanel1.TabIndex = 2;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(487, 377);
            this.Controls.Add(this.dockPanel1);
            this.Controls.Add(this.menuStrip1);
            this.IsMdiContainer = true;
            this.Name = "MainForm";
            this.Text = "Test Designer";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public static MainForm Instance
        {
            get
            {
                return _instance;
            }
        }

        public WorkQueue WorkQueue
        {
            get
            {
                return Helper.GetWorkQueue();
            }
        }
    }
}
